using System.Collections.Generic;
using UnityEngine;

namespace Voxels {
	public static class StaticResources {
		public static BlockInfoProvider BlocksInfo;

		public static Dictionary<ushort, Mesh> BlockModelCache = new Dictionary<ushort, Mesh>();

		public static Mesh GetBlockMesh(BlockData data) {
			ushort key = (ushort)(((byte)data.Type) * 256 + data.Subtype);
			if ( BlockModelCache.TryGetValue(key, out var mesh) ) {
				return mesh;
			}
			mesh = GenBlockMesh(data);
			BlockModelCache.Add(key, mesh);
			return mesh;
		}

		public static Mesh GenBlockMesh(BlockData data) {
			var genMesh = new GeneratableMesh(64);
			var offset = new Vector3(-0.5f, -0.5f, -0.5f);
			var desc = BlocksInfo.GetBlockDescription(data.Type);
			var inp = new MesherBlockInput() { Block = data, Position = Byte3.Zero, Visibility = VisibilityFlags.All };
			BlockModelGenerator.AddBlock(genMesh, desc, ref offset, ref inp);
			genMesh.BakeMesh();
			return genMesh.Mesh;
		}
	}
}
